package com.ehome.poct.core.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ehome.core.service.AbstractCrudServiceImpl;
import com.ehome.poct.core.api.dto.check.TestRecordDetailDTO;
import com.ehome.poct.core.api.dto.order.TestPoctProfitsRecordDTO;
import com.ehome.poct.core.api.virtual.TestRecordDetailService;
import com.ehome.poct.core.api.vo.checkdata.TestRecordDetailVO;
import com.ehome.poct.core.constant.OrderBizCodeEnums;
import com.ehome.poct.core.repository.check.TestRecordDetailExtRepository;
import com.ehome.poct.core.repository.order.TestOrderUnificationPayRepository;
import com.ehome.poct.core.utils.EHomeAssert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 检测记录ServiceImpl
 *
 * @author ehome
 * @Date 2022-03-19 16:15
 */
@Service
@AllArgsConstructor
@Slf4j
public class TestRecordDetailServiceImpl extends AbstractCrudServiceImpl<TestRecordDetailDTO> implements TestRecordDetailService {
    private final TestRecordDetailExtRepository extRepository;

    private final TestOrderUnificationPayRepository unificationPayRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRecord(List<TestRecordDetailDTO> records) {
        final var recordExt = extRepository.getAgentInfo(records.get(0).getAgentId());
        records.forEach(r -> {
            this.insert(r);
            recordExt.setRecordId(r.getRecordId());
            extRepository.insert(recordExt);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TestRecordDetailVO updateRecordInfo(final TestRecordDetailDTO recordDetailDTO) {
        EHomeAssert.notNull(recordDetailDTO, OrderBizCodeEnums.ORDER_RECORD_DETAILS_IS_NULL);
        EHomeAssert.notNull(recordDetailDTO.getOrderNo(), OrderBizCodeEnums.NOT_FOUND_TEST_ORDER);

        List<TestRecordDetailDTO> detailDTOList = list(new TestRecordDetailDTO().setOrderNo(recordDetailDTO.getOrderNo()).setStripTypeCode(recordDetailDTO.getStripTypeCode()));
        EHomeAssert.isTrue(CollUtil.isNotEmpty(detailDTOList), OrderBizCodeEnums.ORDER_RECORD_DETAILS_NOT_FOUND, recordDetailDTO.getOrderNo());
        TestRecordDetailDTO detailDTO = detailDTOList.get(0);
        recordDetailDTO.setRecordId(detailDTO.getRecordId());

        List<TestPoctProfitsRecordDTO> recordDTOList = unificationPayRepository.getShareAccountList(recordDetailDTO.getOrderNo());
        EHomeAssert.isTrue(CollUtil.isNotEmpty(recordDTOList), OrderBizCodeEnums.ORDER_PROFIT_NOT_FOUND, recordDetailDTO.getOrderNo());

        Optional<TestPoctProfitsRecordDTO> optional = recordDTOList.stream()
                .filter(profitsRecordDTO -> Objects.nonNull(profitsRecordDTO.getStripTypeCode()))
                .filter(profitsRecordDTO -> profitsRecordDTO.getStripTypeCode().equals(recordDetailDTO.getStripTypeCode()))
                .findFirst();

        optional.ifPresent(profitsRecordDTO -> {
            recordDetailDTO.setProfitRecordId(profitsRecordDTO.getProfitsRecordId());
            this.update(recordDetailDTO.getRecordId(), recordDetailDTO);
        });
        return BeanUtil.copyProperties(recordDetailDTO, TestRecordDetailVO.class);
    }
}